#include <gtest/gtest.h>

#include "../src/dbmemory.hpp"

#include <cstdlib>
#include <iostream>

class TestDBMemory : public ::testing::Test{
protected:
	void SetUp() override{
		db = new DBMemory(20);
	}

	void TearDown() override{
		delete db;
	}

	DBMemory *db;
};

TEST_F(TestDBMemory, main){
	ASSERT_EQ(db->size(), 0);
	ASSERT_TRUE(db->empty());
}

TEST_F(TestDBMemory, erase){
	int size = rand()%500 + 123;
	uint16_t data[10];
	db->put((char *)data, sizeof(data));
	ASSERT_GE(db->size(), 1);

	db->erase();
	ASSERT_EQ(db->size(), 0);
	ASSERT_TRUE(db->empty());
}

TEST_F(TestDBMemory, multiput){
	int size = rand()%500 + 123;
	uint16_t data[10];
	for( auto i = 0; i < size; ++i ){
		for( auto j = 0; j < sizeof(data)/sizeof(*data); ++ j){
			data[j] = rand()%50;
		}
		db->put((char *)data, sizeof(data));
		ASSERT_EQ(db->size(), i+1);
	}
	ASSERT_EQ(db->size(), size);
}

TEST_F(TestDBMemory, putget){
	int size = rand()%500 + 123;
	uint16_t data[10];
	for( auto i = 0; i < size; ++i ){
		db->put((char *)data, sizeof(data));
		ASSERT_EQ(db->size(), 1);
		db->get((char *)data, sizeof(data));
	}
	ASSERT_EQ(db->size(), 0);
	ASSERT_TRUE(db->empty());
}

TEST_F(TestDBMemory, get){
	ASSERT_FALSE(db->get(nullptr, 20));
	uint16_t data[10];
	db->put((char *)data, sizeof(data));
	ASSERT_TRUE(db->get((char *)data, 20));
	ASSERT_FALSE(db->get((char *)data, 20));
}
